package com.own.component.util.time;

import com.own.component.util.time.base.DateTimeBaseUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

/**
 * ZonedDateTimeUtil
 *
 * @author chenxueli
 * @date 2023-06-28 11:21:00
 */
public class ZonedDateTimeUtil {

    /**
     * localDateTime转ZonedDateTime
     *
     * @param localDateTime localDateTime
     * @return ZonedDateTime
     */
    public static ZonedDateTime of(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return ZonedDateTime.ofInstant(localDateTime.toInstant(ZoneOffset.ofHours(8)), ZoneOffset.ofHours(-4));
    }

    /**
     * 格式化时间
     *
     * @param text     时间字符串
     * @param patterns 格式化的字段串
     * @return 时间
     */
    public static ZonedDateTime parse(String text, String... patterns) {
        var pattern = DateTimeBaseUtil.buildPattern(patterns);
        var formatter = DateTimeBaseUtil.getDateTimeFormatter(pattern);
        return ZonedDateTime.parse(text, formatter);
    }

}
